package org.tx.paypal.client;

import com.paypal.http.HttpResponse;
import com.paypal.http.serializer.Json;
import com.paypal.orders.*;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import java.io.IOException;

@Slf4j
public class CaptureOrder extends PayPalClient {

	private String clientId = "ARhzyOahSH227z0VIxqfjtkJ4ShCxv3HtbskgttUZJRngsmfrvs3SKh_hWk-9wFi2N1fy8sy0-n527u9";
	private String clientSecret = "EN2TdvNlmT5F5dH9ZfeB8EuPX-UGJ1-rwh4lgyI8HShws6sRoBDWTJIjL7XZCnvi9WaM12YFhdQCpLul";
	private String mode = "sandbox";

	public static void main(String[] args) {
		try {//"订单id，CreateOrder 生成"
			new CaptureOrder().captureOrder("56P35780DT925653Y");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public OrderRequest buildRequestBody() {
		return new OrderRequest();
	}

	/**
	 * 用户授权支付成功，进行扣款操作
	 */
	public HttpResponse<Order> captureOrder(String orderId) throws IOException {
		OrdersCaptureRequest request = new OrdersCaptureRequest(orderId);
		request.requestBody(new OrderRequest());
		PayPalClient payPalClient = new PayPalClient();
		HttpResponse<Order> response = null;
		try {
			response = payPalClient.client(mode, clientId, clientSecret).execute(request);
		} catch (IOException e1) {
			try {
				log.error("第1次调用paypal扣款失败");
				response = payPalClient.client(mode, clientId, clientSecret).execute(request);
			} catch (Exception e) {
				try {
					log.error("第2次调用paypal扣款失败");
					response = payPalClient.client(mode, clientId, clientSecret).execute(request);
				} catch (Exception e2) {
					log.error("第3次调用paypal扣款失败，失败原因 {}", e2.getMessage());
				}
			}
		}
		log.info("Status Code = {}, Status = {}, OrderID = {}", response.statusCode(), response.result().status(), response.result().id());
		for (LinkDescription link : response.result().links()) {
			log.info("Links-{}: {}    \tCall Type: {}", link.rel(), link.href(), link.method());
		}
		for (PurchaseUnit purchaseUnit : response.result().purchaseUnits()) {
			for (Capture capture : purchaseUnit.payments().captures()) {
				log.info("Capture id: {}", capture.id());
				log.info("status: {}", capture.status());
				log.info("invoice_id: {}", capture.invoiceId());
				if ("COMPLETED".equals(capture.status())) {
					//进行数据库操作，修改订单状态为已支付成功，尽快发货（配合回调和CapturesGet查询确定成功）
					log.info("支付成功,状态为=COMPLETED");
				}
				if ("PENDING".equals(capture.status())) {
					log.info("status_details: {}", capture.captureStatusDetails().reason());
					String reason = "PENDING";
					if (capture.captureStatusDetails() != null && capture.captureStatusDetails().reason() != null) {
						reason = capture.captureStatusDetails().reason();
					}
					//进行数据库操作，修改订单状态为已支付成功，但触发了人工审核，请审核通过后再发货（配合回调和CapturesGet查询确定成功）
					log.info("支付成功,状态为=PENDING : {}", reason);
				}
			}
		}
		Payer buyer = response.result().payer();
		log.info("Buyer Email Address: {}", buyer.email());
		log.info("Buyer Name: {} {}", buyer.name().givenName(), buyer.name().surname());
		String json = new JSONObject(new Json().serialize(response.result())).toString(4);
		log.info("captureOrder response body: {}", json);
		return response;
	}

}

